package com.railway.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class testServlet
 */
@WebServlet("/testServlet.do")
public class testServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
//		String date = request.getParameter("date");
//		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
//		Date d= null;
//		
//		try {
//			d = dft.parse(date);
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		long long1 = d.getTime();
//		long long2 = (new Date()).getTime();
//		System.out.println(dft.format(d));
		
		String date1 = request.getParameter("date");
		
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date d = null;
		try {
			d = dft.parse(date1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		date1 += " 23:59:59";
		Date date2 = null;
				try {
					date2 =dft.parse(date1);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		Date date = new Date();
		dft = new SimpleDateFormat("yyyy-MM-dd");
		String da = dft.format(date);
		Date lastDate = null;
		try {
			lastDate = dft.parse(da);
			long lastlong = lastDate.getTime();
			lastlong +=1000*60*60*24*4;
			lastDate = new Date(lastlong);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		long long1 = d.getTime();
		long long2 = date.getTime();
		if((long2 - long1) > 0) {
			d = date;
		}else if((long1 - long2) > (1000*60*60*24*5)) {
			d = null;
		}else if((long1 - long2) < (1000*60*60*24*5) && (long1 - long2) > 0) {
			d = d;
		}
		System.out.println(d);
		System.out.println(date2);
		System.out.println(lastDate);
	}
}
